package com.liu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.liu.dao.TPatientDao;
import com.liu.dto.PatientMapData;
import com.liu.dto.PatientType;
import com.liu.entity.TPatient;
import com.liu.service.TPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 患者表(TPatient)表服务实现类
 *
 * @author makejava
 * @since 2022-11-04 17:48:44
 */
@Service("tPatientService")
public class TPatientServiceImpl implements TPatientService {



    @Autowired
    private TPatientDao tPatientDao;


   public Integer getAsymptomatic(){
       return this.tPatientDao.getAsymptomatic();
    };

    @Override
    public Integer getMiJie() {
        return this.tPatientDao.getMiJie();
    }

    @Override
    public List<PatientMapData> getPatientTPatientMap() {
        return tPatientDao.getPatientMapData();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TPatient queryById(Integer id) {
        return this.tPatientDao.queryById(id);
    }



    /**
     * 新增数据
     *
     * @param tPatient 实例对象
     * @return 实例对象
     */
    @Override
    public TPatient insert(TPatient tPatient) {
        this.tPatientDao.insert(tPatient);
        return tPatient;
    }

    /**
     * 修改数据
     *
     * @param tPatient 实例对象
     * @return 实例对象
     */
    @Override
    public TPatient update(TPatient tPatient) {
        this.tPatientDao.update(tPatient);
        return this.queryById(tPatient.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tPatientDao.deleteById(id) > 0;
    }

    @Override
    public List<PatientType> getPatientType() {
        return this.tPatientDao.getPatientType();
    }

    @Override
    public List<TPatient> getAll() {
        return this.tPatientDao.getAll();
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        this.tPatientDao.deleteByIds(ids);
    }

    @Override
    public void selectByIds(Integer[] ids) {
        this.tPatientDao.queryByIds(ids);
    }
    @Override
    public void writeExcel(Integer[] ids, HttpServletResponse response) throws UnsupportedEncodingException {
        //判断
        if(ids.length > 0){
            //查询数据库
            List<TPatient> patientList = tPatientDao.queryByIds(ids);
            String time = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());

            System.out.println("当前系统时间戳：" + time);
            String s= "疫情表" + time;

            String fileName = new String(s.getBytes("iso8859-1"), "utf-8");

            fileName += ".xlsx";
            response.setHeader("Content-Disposition","attachment; filename="+fileName);


            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            }catch (IOException e){
                e.printStackTrace();
            }
            EasyExcel.write(outputStream, TPatient.class).sheet("患者列表").doWrite(patientList);

        }else {
        //    如果没有查到数据，就导出所有
            List<TPatient> patientList = tPatientDao.getAll();
            String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String fileName = new String(("疫情防控表" + time).getBytes("iso8859-1"), "utf-8");

            fileName += ".xlsx";

            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);

            ServletOutputStream outputStream = null;

            try {
                outputStream = response.getOutputStream();

            }catch (IOException e) {
                e.printStackTrace();
            }

            EasyExcel.write(outputStream, TPatient.class).sheet("疫情形式表").doWrite(patientList);
        }
    }

    @Override
    public void save(MultipartFile file) {

    }
}
